/********************************************************************************
 *      Copyright:  (C) 2022 Zhang Changxing<ZhangChangxingVIP@163.com>
 *                  All rights reserved.
 *
 *       Filename:  database.h
 *    Description:  This head file 
 *
 *        Version:  1.0.0(04/13/2022)
 *         Author:  Zhang Changxing <ZhangChangxingVIP@163.com>
 *      ChangeLog:  1, Release initial version on "04/13/2022 01:35:13 AM"
 *                 
 ********************************************************************************/


#ifndef  _DATABASE_H_
#define  _DATABASE_H_

/*System library*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>


/*Third-party libraries*/
#include "sqlite3.h"
#include "logger.h"

/*Custom header files*/
#include "conf.h"
#include "database.h"

  



/*---------------------------------------------------------------------------------------------------
 *   description:   Database initialization, open the database and create a table
 *  return value:   <0: failure,    =0:ok 
 *---------------------------------------------------------------------------------------------------*/
int sqlite_init(void);
 

/*---------------------------------------------------------------------------------------------------
 *   description:   Insert the data that failed to send into the table of the database
 *          args:   $pack_info:     Packet structure
 *  return value:   <0: failure,    =0:ok 
 *---------------------------------------------------------------------------------------------------*/
int db_data_insert(pack_info_t pack_info);


/*---------------------------------------------------------------------------------------------------
 *   description:   Delete the first data in the database
 *          args:   $table_name:    Database table name
 *  return value:   <0: failure,    =0:ok 
 *---------------------------------------------------------------------------------------------------*/
int db_data_delete(void);


/*---------------------------------------------------------------------------------------------------
 *   description:   Find the first data from table_name in the database and store it in the pack_info structure
 *          args:   $pack_info:     Packet structure
 *                  $table_name:    Database table name
 *  return value:   <0: failure,    =0:ok 
 *---------------------------------------------------------------------------------------------------*/
int select_first_data(pack_info_t *pack_info);


/*---------------------------------------------------------------------------------------------------
 *   description:   Check whether the table is empty
 *          args:   $table_name:    Database table name
 *  return value:   <0: failure,    =0:ok 
 *---------------------------------------------------------------------------------------------------*/
int check_table_data(void);


/*---------------------------------------------------------------------------------------------------
 *   description:   Determine whether the database is still in use and shut down the database
 *  return value:   <0: failure,    =0:ok 
 *---------------------------------------------------------------------------------------------------*/
int close_database(void);


#endif   /* ----- #ifndef _DATABASE_H_  ----- */


